package com.xinguan.utils;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 陈俊行
 * @Date: 2023/10/31/22:00
 * @Description:
 */
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
    public static String encrypt(String input) {
        try {
            // 创建MessageDigest对象，选择MD5算法
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 将输入字符串转换成字节数组
            byte[] inputBytes = input.getBytes();

            // 计算MD5摘要
            byte[] hash = md.digest(inputBytes);

            // 将摘要转换成十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : hash) {
                String hex = Integer.toHexString(0xFF & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }

            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            // 处理异常
            e.printStackTrace();
            return null;
        }
    }

//    public static void main(String[] args) {
//        String input = "123456";
//        String encrypted = encrypt(input);
//        System.out.println("MD5 Hash: " + encrypted);
//    }
}
